/**
 * 
 */
package com.lanswon.qzsmk.service.systemManageService.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.dao.RoleMapper;
import com.lanswon.qzsmk.dao.RolePrivilegeMapper;
import com.lanswon.qzsmk.dao.SysPermissionMapper;
import com.lanswon.qzsmk.dao.UserMapper;
import com.lanswon.qzsmk.dao.UserRoleAdminMapper;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.ErrorCode;
import com.lanswon.qzsmk.model.Role;
import com.lanswon.qzsmk.model.RolePrivilege;
import com.lanswon.qzsmk.model.SysPermission;
import com.lanswon.qzsmk.model.User;
import com.lanswon.qzsmk.model.UserRoleAdmin;
import com.lanswon.qzsmk.service.systemManageService.RoleManageService;
import com.lanswon.qzsmk.vo.QueryRoleVo;
import com.lanswon.qzsmk.vo.UserRoleVo;

/**
 * @author sun
 *
 */
@Service
public class RoleManageServiceImpl implements RoleManageService {

	@Autowired
	RoleMapper rm;

	@Autowired
	UserRoleAdminMapper uram;

	@Autowired
	UserMapper um;
	
	@Autowired
	SysPermissionMapper spm;
	
	@Autowired
	RolePrivilegeMapper rpm;

	@Override
	public void addRole(Role r) throws ApplicationException {
		Role o = rm.queryFirstRole();
		if (o == null) {
			r.setRoleNo("10000");
		} else {
			if(rm.queryRoleByRoleName(r.getRoleName()) != null ){
				throw new ApplicationException(ErrorCode.ROLE_NAME_EXIST, "RoleName [ "+r.getRoleName()+" already exist!!! ] ");
			}
			Integer a = Integer.valueOf(o.getRoleNo()) + 1;
			r.setRoleNo(String.valueOf(a));
		}
		
	
		rm.insert(r);
		
		

	}

	@Override
	public void deleteRoles(List<Integer> roleIds) throws Exception {
		if (roleIds.size() == 0) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "roleId is null !!!");
		}
		
		
		for (Integer roleId : roleIds) {
			List<UserRoleAdmin> list = uram.queryUserRoleAdminByRoleId(roleId);
			if(list.size()>0){
				throw new ApplicationException(ErrorCode.ROLE_BIND_USER, "role bind user!!!");
			}
			List<RolePrivilege> l =rpm.queryRolePrivilegeByRoleId(roleId);
			if(l.size()>0){
				throw new ApplicationException(ErrorCode.ROLE_BIND_PRIVILEGE, "role bind Privilege!!!");
			}
			
			
			rm.deleteByRoleId(roleId);
		}
	}

	@Override
	public DwzPage<Role> queryRole(QueryRoleVo qrv) {

		List<Role> list = rm.LikeQueryRoleByParam(qrv);

		int totalCount = rm.LikeQueryRoleCountByParam(qrv);

		DwzPage<Role> dp = new DwzPage<>(qrv.getCurrentPage(), qrv.getNumPerPage(), totalCount);

		dp.setList(list);

		return dp;
	}

	@Override
	public void addUser(Integer userNo, Integer systermId, Integer roleId, String isAdd) {
		User user = um.queryUserByNo(userNo);
		UserRoleAdmin ura = new UserRoleAdmin(roleId, systermId, user.getUserId());
		if (isAdd.equals("0")) {// 取消
			uram.delete(ura);
		} else if (isAdd.equals("1")) {// 添加
			uram.insert(ura);
		}
	}

	@Override
	public DwzPage<UserRoleVo> showUsers(UserRoleVo vo) throws Exception {
		List<UserRoleVo> list = um.queryUserByRole(vo);
		int totalCount = um.queryUserCountByRole(vo);
		DwzPage<UserRoleVo> dp = new DwzPage<UserRoleVo>(vo.getCurrentPage(), vo.getNumPerPage(), totalCount);
		dp.setList(list);
		return dp;
	}

	@Override
	public Role queryRoleById(Integer roleId) {
		
		return rm.queryRoleById(roleId);
	}

	
	@Override
	public void batchInsert(List<RolePrivilege> list) {
		 rpm.batchInsert(list);
	}
	
	

}
